package com.topc.problem.api;

import cn.hutool.core.util.ObjectUtil;
import com.topc.base.api.CommonResult;
import com.topc.base.exception.Asserts;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.model.UserAuthDto;
import com.topc.problem.model.dto.*;
import com.topc.problem.model.po.ProblemComposeInfo;
import com.topc.problem.model.po.ProblemTag;
import com.topc.problem.model.vo.*;
import com.topc.problem.service.*;
import com.topc.problem.util.FileToSamplesUtils;
import com.topc.problem.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/close")
public class ProblemCloseController {

    @Autowired
    private IProblemTagService problemTagService;

    @Autowired
    private IProblemInfoService problemInfoService;

    @Autowired
    private IProblemComposeInfoService problemComposeInfoService;

    @Autowired
    private IProblemDealContentService problemDealContentService;

    @Autowired
    private IProblemDealInfoService problemDealInfoService;

    @Autowired
    private  IProblemDealCommentInfoService problemDealCommentInfoService;

    @Autowired
    private IProblemUserMidService problemUserMidService;


    @PostMapping("/tag/update")
    public CommonResult<String> updateTag(@RequestBody ProblemTag problemTag) {
        problemTagService.updateTagInfo(problemTag);
        return CommonResult.success("更新成功");
    }

    @GetMapping("/tag/del")
    public CommonResult<String> delTag(Long tagId) {
        problemTagService.delTagInfo(tagId);
        return CommonResult.success("删除成功");
    }

    @PostMapping("/tag/save")
    public CommonResult<String> addTagInfo(@RequestBody @Validated ProblemTag problemTag) {
        problemTagService.addTagInfo(problemTag);
        return CommonResult.success("添加成功");
    }

    @PostMapping("/search-info")
//    @PreAuthorize("hasAuthority('admin_problem_view')")//限制problem权限才可访问
    public CommonResult<PageResult<ProblemPageShowVo>> searchProblemInfoByPage(PageParams pageParams,
                                                                               @RequestBody @Validated  QueryProblemInfoDto queryProblemInfoDto) {
        PageResult<ProblemPageShowVo> problemPageShowVoPageResult = problemInfoService.searchByPageToAdmin(pageParams, queryProblemInfoDto);
        return CommonResult.success(problemPageShowVoPageResult);
    }

    @GetMapping("/detail-info")
    @PreAuthorize("hasAuthority('admin_problem_view')")
    public CommonResult<ProblemDetailVo> getDetailProblemInfoById(@RequestParam("problemId") @NotNull Long problemId){
        ProblemDetailVo problemInfo = problemInfoService.getDetailById(problemId);
        return CommonResult.success(problemInfo);
    }

    @PostMapping("/info/save")
    @PreAuthorize("hasAuthority('admin_problem_insert')")
    public CommonResult<Long> saveProblemInfo(@RequestBody @Validated SaveProblemDto saveProblemDto) {
        Long newId = problemInfoService.saveProblem(saveProblemDto);
        return CommonResult.success(newId);
    }

    @PostMapping("/info/update-info")
    @PreAuthorize("hasAuthority('admin_problem_update')")
    public CommonResult<Long> updateProblemInfo(@RequestBody @Validated SaveProblemDto saveProblemDto) {
        if (ObjectUtil.isEmpty(saveProblemDto.getId())) return CommonResult.failed("数据有误");
        Long id = problemInfoService.updateProblemInfo(saveProblemDto);
        return CommonResult.success(id);
    }

    @PostMapping("/info/update-sample")
    @PreAuthorize("hasAuthority('admin_problem_update')")
    public CommonResult<Long> updateProblemSample(@RequestParam("problemId") @NotNull Long problemId, @RequestParam("files") MultipartFile[] files) {
        Long id = problemInfoService.updateProblemSamples(problemId, FileToSamplesUtils.getProblemSampleByFile(files));
        return CommonResult.success(id);
    }



    @PostMapping("/compose/save")
    public CommonResult<Long> saveProblemCompose(@RequestBody @Validated SaveProblemComposeDto composeDto) {
        composeDto.setId(null);
        composeDto.setUserNumber(SecurityUtil.getUser().getNumber());
        Long id = problemComposeInfoService.addProblemCompose(composeDto);
        return CommonResult.success(id);
    }

    @PostMapping("/compose/update")
    public CommonResult<Long> updateProblemCompose(@RequestBody @Validated SaveProblemComposeDto composeDto) {
        if (ObjectUtil.isEmpty(composeDto.getId())) return CommonResult.failed("参数传递有误");
        Long id = problemComposeInfoService.saveProblemCompose(composeDto);
        return CommonResult.success(id);
    }

    @PostMapping("/search-compose")
    public CommonResult<PageResult<ProblemComposeInfo>> searchComposeByPageToAdmin(PageParams pageParams,@RequestBody @Validated QueryComposeInfoDto queryProblemComposeDto) {
        PageResult<ProblemComposeInfo> problemComposeInfoPageResult = problemComposeInfoService.searchByPage(pageParams, queryProblemComposeDto,1);
        return CommonResult.success(problemComposeInfoPageResult);
    }

    @GetMapping("/detail-compose")
    public CommonResult<ComposeShowVo> getComposeDetailToAdmin(@NotNull Long id){
        UserAuthDto user = SecurityUtil.getUser();
        ComposeShowVo composeShowVo  = problemComposeInfoService.getComposeDetail(id, user.getNumber(),user.getRoleType());
        return CommonResult.success(composeShowVo);
    }

    @GetMapping("/compose/delete")
    public CommonResult<Long> deleteComposeInfoById(@NotNull Long id){
        Long infoId =  problemComposeInfoService.deleteInfoById(id);
        return CommonResult.success(infoId);
    }


    @PostMapping("/deal/save")
    public CommonResult<String> saveDealInfo(@RequestBody @Validated SaveDealInfoDto saveDealInfoDto){
        saveDealInfoDto.setUserNumber(SecurityUtil.getUser().getNumber());
        problemDealInfoService.saveDealInfo(saveDealInfoDto);
        return CommonResult.success("新增成功");
    }
    //文章信息-修改
    @PostMapping("/update-deal")
    public CommonResult<String> updateDealInfoById(@NotNull Long id,@RequestBody @Validated SaveDealInfoDto saveDealInfoDto){
        saveDealInfoDto.setUserNumber(SecurityUtil.getUser().getNumber());
        problemDealInfoService.updateInfoById(id,saveDealInfoDto);
        return CommonResult.success("修改成功");
    }
    //文章信息-删除
    @GetMapping("/deal/delete")
    public CommonResult<String> deleteDealInfoById(@NotNull Long id){
        //通过id找到题解内容id，并删除题解内容
        problemDealInfoService.deleteInfoById(id);
        //删除题解基本信息
        return CommonResult.success("删除成功");
    }

    /**
     * TODO
     * @param pageParams
     * @param id
     * @return
     */
    //题解信息查看
    @PostMapping("/search-deal")
    public CommonResult<PageResult<DealShowVo>> searchDealInfoByPage(PageParams pageParams, @NotNull Long id){
        PageResult<DealShowVo> pageResult = problemDealInfoService.searchDealByPage(pageParams, id);
        return CommonResult.success(pageResult);
    }

    /**
     * TODO
     * @param id
     * @return
     */
    @GetMapping("/get-deal-detail")
    public CommonResult<DealDetailShowVo> getInfoDetailById(@NotNull Long id){
        DealDetailShowVo dealDetailShowVo = problemDealInfoService.searchDetailById(id);
        return CommonResult.success(dealDetailShowVo);
    }

    //题解评论查看
    @PostMapping("/search-deal-comment")
    public CommonResult<PageResult<DealCommentShowVo>> searchCommentByNumber(PageParams pageParams, @NotNull String userNumber,@NotNull Long id){
        PageResult<DealCommentShowVo> pageResult = problemDealInfoService.searchCommentByNumber(pageParams, userNumber,id);
        return CommonResult.success(pageResult);
    }

    //增加评论
    @PostMapping("/save-deal-comment")
    public CommonResult<String> saveCommentInfo(@RequestBody @Validated SaveDealCommentDto saveDealCommentDto){
        saveDealCommentDto.setUserNumber(SecurityUtil.getUser().getNumber());
        problemDealInfoService.saveCommentInfo(saveDealCommentDto);
        return CommonResult.success("评论成功");
    }

    //删除评论
    @GetMapping("/delete-deal-comment")
    public CommonResult<String> deleteCommentById(@NotNull Long id){
        problemDealCommentInfoService.deleteCommentInfoById(id);
        return CommonResult.success("删除成功");
    }

    /**
     * TODO
     * @param userNumber
     * @param problemId
     * @param CompetitionId
     * @return
     */
    //查询做题结果记录(题目提交记录界面)
    @PostMapping("/search-problem-record-result")
    public CommonResult<List<ProblemCommitShowVo>> searchCodeListResult(@NotNull String userNumber,@NotNull Long problemId,@NotNull Long CompetitionId){
        if (!userNumber.equals(SecurityUtil.getUser().getNumber())){
            Asserts.fail("无权限查看");
        }
        List<ProblemCommitShowVo> problemCommitList=problemUserMidService.getProblemCommitList(userNumber,problemId,CompetitionId);
        return CommonResult.success(problemCommitList);
    }

    /**
     * TODO
     * @param
     * @param competitionId
     * @return
     */
    //查看题单信息/查询做题最终结果记录(竞赛题目列表/作业题单界面)
    @GetMapping("/search-problem-list-result")
    public CommonResult<List<ProblemListShowVo>> searchProblemListResult(@NotNull Long competitionId){
        System.out.println("competitionId:"+competitionId);
        String userNumber = SecurityUtil.getUser().getNumber();
        List<ProblemListShowVo> problemListShowVo=problemInfoService.getProblemList(userNumber,competitionId);
        return CommonResult.success(problemListShowVo);
    }

    //题目详细内容查询(做题界面)
    @GetMapping("/search-detail")
    public CommonResult<ProblemDetailShowVo> searchProblemDetailByPage(@NotNull Long problemId) {
        ProblemDetailShowVo problemDetailShowVo = problemInfoService.searchDetailById(problemId);
        return CommonResult.success(problemDetailShowVo);
    }
}
